//
//  服务器配置 - 原先是写在了常量中 不利于配置修改
//
//

//
// TMCache 是否有配置 服务器地址
//     有 - 则使用
//     无 - 找 AppServiceData 找寻
//

//
//  AppServiceData GetDefault 方法 首先将所有医院写到数组中，按 打包 时的配置的  bulid 号 索到对应的 配置
//

#import "AppServiceConfig.h"
#import "AppServiceData.h"
#import <TMCache.h>

@implementation AppServiceConfig


+(NSString*) GetServiceUrl{
    
    AppServiceConfig *_ = [[AppServiceConfig alloc]init];
    AppServiceModel* config =  [_ GetServiceConfigModel];
    
    NSString *ip = config.IPAddressOrDomainName;
    NSString *port = config.Port;
    NSString *iis = config.IISApplicationName;
    if(![iis isEqualToString:@""]){
        iis = [NSString stringWithFormat:@"/%@",iis];
    }
    NSString *url = [NSString stringWithFormat:@"http://%@:%@%@/hr/appservice",ip,port,iis];
//    NSString *token = [[TMCache sharedCache]objectForKey:@"deviceToken"];
//    if (token.length > 0) {
//        url = [NSString stringWithFormat:@"http://%@:%@%@/hr/appservice/token=%@",ip,port,iis,token];
//
//    }
    return url;
}

+(void) SetServiceUrl:(AppServiceModel*)config{
    
    NSString *ip = config.IPAddressOrDomainName;
    NSString *port = config.Port;
    NSString *iis = config.IISApplicationName;
    
    
    NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:ip,@"CONFIG_IP",port,@"CONFIG_PORT",iis,@"CONFIG_IIS", nil];
    [[TMCache sharedCache]setObject:dict forKey:@"APP_SERVICE_CONFIG"];
}

+(void) ClearServiceUrl{
    [[TMCache sharedCache] removeObjectForKey:@"APP_SERVICE_CONFIG"];
}



-(AppServiceModel*) GetServiceConfigModel{
    
    NSDictionary *dict = [[TMCache sharedCache] objectForKey:@"APP_SERVICE_CONFIG"];
    
    if(dict != nil){
        NSString *ip = [dict objectForKey:@"CONFIG_IP"];
        NSString *port = [dict objectForKey:@"CONFIG_PORT"];
        NSString *iis = [dict objectForKey:@"CONFIG_IIS"];
        
        AppServiceModel* model = [[AppServiceModel alloc]init];
        model.IPAddressOrDomainName = ip;
        model.Port = port;
        model.IISApplicationName = iis;
        return model;
    }
    
    AppServiceData *data = [[AppServiceData alloc]init];
    return [data GetDefault];
    
    
}

@end
